<style scoped>
   .doclist .shark {
        padding: 0 30px;
    }

   .doclist .fast {
        font-size: 20px;
        color: #f39312;
        letter-spacing: 1px;
    }

    .doclist .main .ivu-select,
    .main .ivu-input {
        border-radius: 0 2px 2px 0;
    }

    .doclist .common-box-body {
        padding-top: 24px;
    }
    .doclist .fixed-bottom{
        padding-left: 30px;
        padding-right: 40px ;
        height: 80px;
        line-height: normal !important;
        position: fixed;
        left: 210px;
        right: 0;
        bottom: 0;
        z-index: 999;
        text-align: center;
        background-color: rgb(255, 255, 255);
        box-shadow: 0px 0px 9px 0px rgba(0, 0, 0, 0.28);
    }
    .doclist .sidehide{
        left: 75px;
    }
    .doclist .detail{
        margin-top: 20px ;
        margin-bottom: 15px !important;
    }
</style>
<style>
    .doclist .common-box-body .ivu-select-single .ivu-select-selection .ivu-select-placeholder,
    .common-box-body .ivu-select-single .ivu-select-selection .ivu-select-selected-value {
        padding-right: 0px;
    }

    .doclist .ivu-tabs.ivu-tabs-card>.ivu-tabs-bar .ivu-tabs-tab {
        color: #2d8cf0;
        border-color: transparent;
        background-color: #FFF;
    }

    .doclist .ivu-tabs.ivu-tabs-card>.ivu-tabs-bar .ivu-tabs-tab-active {
        color: #808080;
        border: 1px solid #dddee1;
        border-bottom: 0;
    }
    .doclist button.ivu-btn{
        border-radius:2px !important;
    }
</style>


<template>
    <div class="doclist">
        <product-title></product-title>
        <div class="shark"  style="padding:0 30px;">
            <div class="fast">快速搜索</div>
            <div class="common-box-body">
                <Form >
                    <Row>
                        <Col :md="24" :lg="4">
                            <FormItem >
                                <Input placeholder="请输入机构简称"  v-model="formdata.shortn"></Input>
                            </FormItem>
                        </Col>
                        <Col :md="24" :lg="4">
                            <FormItem >
                                <Input placeholder="请输入机构代码" v-model="formdata.agency_code"></Input>
                            </FormItem>
                        </Col>
                        <Col :md="24" :lg="4">
                            <FormItem >
                                <Select placeholder="选择机构类型"  v-model="formdata.cate">
                                    <Option value=" ">全部</Option>    
                                    <Option :value="item.name" v-for="item in cateList" :key="item.name">{{item.name}}</Option>                                    
                                </Select>
                            </FormItem>
                        </Col>
                        <Col :md="24" :lg="4">
                            <FormItem >
                                <Select placeholder="选择机构状态"  v-model="formdata.status">
                                    <Option value="营业">营业</Option>                                  
                                    <Option value="停业">停业</Option>                                  
                                </Select>
                            </FormItem>
                        </Col>
                        <Col :md="24" :lg="4">
                            <FormItem >
                                <Select placeholder="选择持证状态"  v-model="formdata.holder">
                                    <Option value="0">全部</Option>
                                    <Option value="1">有证</Option>                                  
                                    <Option value="2">备案</Option>                           
                                </Select>
                            </FormItem>
                        </Col>
                        <Col :md="24" :lg="4">
                            <Button size="large" class="fr" type="primary" icon="ios-search" @click="search">搜索</Button>
                        </Col>
                    </Row>
                    <div style="border-bottom:1px solid #BEBEBE;"></div>
                    <Row style="margin-top:20px;">
                        <!-- <Col :md="8" :lg="2" offset='16'>
                            <Button size="large" class="fr" type="info" icon="ios-compose-outline">导出列表</Button>
                        </Col>
                        <Col :md="8" :lg="2" offset='1'>
                            <Button type="primary" size="large" class="fr" icon="icon iconfont icon-riqi">表头设置</Button>
                        </Col> -->
                        <!--:md="8"  offset='1' -->
                        <Col :md="24" :lg="2" offset='22'>
                            <Button size="large" class="fr" v-if="$isShow('yun_org/create2')" type="warning" icon="plus" @click="addnewdoclist">新增</Button>
                        </Col>
                    </Row>
                </Form>
            </div>
            <div class="detail">
                <div class="clearfix">
                    <div class="table-theme highlight_table">
                        <Table  ref="currentRowTable" @on-current-change="tableCurrentChange($event)" size="large" stripe highlight-row border :columns="columns1" :data="policylist" class=""></Table>
                    </div>                        
                </div>
            </div>
            <div :class="slidetoggle?'fixed-bottom':'fixed-bottom sidehide' ">
                <Row class="pro-pagesize">
                    <Col :md="24" :lg="12">
                        <Button type="error" v-if="$isShow('yun_org/delete')" :disabled="tableaction" size="large" class="fl mr10 " @click="remove">删除</Button>
                        <Button type="info" v-if="$isShow('yun_org/transfer_data')"  :disabled="tableaction" size="large" class="fl mr10" @click="transForm">批量转移机构数据</Button>
                        <Button type="info" v-if="$isShow('yun_org/get_one')" :disabled="tableaction" size="large" class="fl mr10" @click="toDetail">编辑</Button>
                        <!-- <Button type="success" :disabled="tableaction" size="large" class="fl mr10" @click="modalTrue">设置账号</Button> -->
                    </Col>
                    <Col :md="24" :lg="12" class="tRight">
                        <Page :total="detailTotal" ref='pages' :current="currentpage" show-total show-sizer :page-size="pagesize" @on-change="pageChange" @on-page-size-change="pageSizeChange" placement="top" class="page"></Page>
                    </Col>
                </Row>
            </div>
        </div>
        <Modal v-model="showAddModal" ok-text="提交" :mask-closable='closeable' width="600" title="批量转移机构数据" @on-ok="add" @on-cancel="cancel" :loading="loading" class-name="common-box-body">
            <Row>
                <Col>
                    <Form :model="org" ref="org" :rules="ruleValidate"  >
                        <Form-item label="机构" prop="org_to" required>
                            <Select  filterable  placeholder="请选择机构" v-model="org.org_to" :transfer=true>
                                <Option v-for="item in filterOrgList" :value="item.id" :key="item.id">{{item.shortn}}</Option>                                                                                                                                       
                            </Select>
                        </Form-item>
                    </Form>
                </Col>
            </Row>
        </Modal>
    </div>
</template>

<script>
    import productTitle from '../../components/productTitle'
    import {delete_org,get_list,get_account,set_yun_account} from '../../api/org'
    import { PermissionGetAll } from '../../api/yun_permission'
    import { getCateList , getListByName , transferData} from '../../api/yun_org'
    export default {
        data() {
            const validatePass = (rule, value, callback) => {
                if (value === '') {
                    callback(new Error('请输入密码'));
                } else {
                    if (this.org.pwd_two !== '') {
                        // 对第二个密码框单独验证
                        this.$refs.org.validateField('pwd_two');
                    }
                    callback();
                }
            };
            const validatePassCheck = (rule, value, callback) => {
                if (value === '') {
                    callback(new Error('请再次输入密码'));
                } else if (value !== this.org.pwd) {
                    callback(new Error('两次输入的密码不匹配!'));
                } else {
                    callback();
                }
            };
            return {
                p:1,//对页面页数的定义-->初始化
                pagesize:10,//对页面的定义,出事定义10条数据
                detailTotal: 0,
                showAddModal:false,//点击设置,弹出模态框
                closeable: false,//shif按ese按钮退出
                loading: true,
                tableaction:true, 
                jiance:false,
                currentpage:1,
                formdata:{
                    p:1,//对页面页数的定义-->初始化
                    pagesize:10,//对页面的定义,出事定义10条数据
                    agency_code:"",//机构代码
                    shortn:'',//机构简称
                    cate:'',//公司类型
                    status:'营业',//营业状态
                } ,
                policylist:[],
                columns1:[
                    {
                    title: '机构简称',
                    key: 'shortn',
                    align: 'center',
                    },
                    {
                    title: '机构代码',
                    key: 'agency_code',
                    align:'center',
                        },
                    {
                        title: '机构电话',
                        key: 'tel',
                        align:'center'
                    },
                    {
                        title: '负责人',
                        key: 'principal',
                        align:'center'
                    },
                    {
                        title: '设立时间',
                        key: 'reg_date',
                        align:'center'
                    },
                    {
                        title: '状态',
                        key: 'status',
                        align:'center'
                    },
                    // {
                    // title: '账号名',
                    // key: 'account_name',
                    // align:'center',
                    // },
                    {
                        title: '注册地',
                        key: 'registe_addr',
                        align:'center'
                    },
                    // {
                    //     title: '角色',
                    //     key: 'role_name',
                    //     align:'center'
                    // },
                    {
                        title: '机构类型',
                        key: 'cate',
                        align:'center'
                    },
                    {
                        title:'持证状态',
                        key:'holder',
                        align:'center',
                        render:(h,params)=>{
                            return h('div',params.row.holder==1?'有证':params.row.holder==2?"备案":'')
                        }
                    }
                ],
                permission_list:[],
                orglist:[],
                cateList:[],
                roleList:[],
                org:{
                    org_from:'',
                    org_to:''
                },
                ruleValidate: {
                    org_to:[
                        { required: true, message: '机构不能为空', trigger: 'blur,change' }
                    ],
                },
                current:{}
            }
        },
        components: {
            productTitle
        },
        computed: {
            slidetoggle(){
                return this.$store.state.sildeshow
            },
            filterOrgList(){
                if(this.current.id){
                    let _arry = []
                    this.orglist.forEach(element => {
                        if(element.id != this.current.id){
                             _arry.push(element)
                         }
                    });
                    return _arry
                }
                
            }
        },
        created() {
            
        },
        mounted(){
            this.getList() ;
            this.PermissionGet();  
            this.getOrgCate();
            this.orglistinit();
        },
        methods: {
            orglistinit(){
                new Promise((resolve, reject) => {
                let _params = {
                    name:''
                }
                getListByName(_params).then(response => {
                        console.log(response.data)
                        if (response.data.err == 0) {               
                            this.orglist = response.data.data;    
                        } else {
                            this.$Message.error(response.data.info);
                        }
                        resolve();
                    }).catch(error => {
                        reject(error);
                    });
                });
            },
            //获取页面信息getList
            getOrgCate(){   
                new Promise((resolve, reject) => {
                    getCateList().then(response => {
                        if (response.data.err == 0) {
                            this.cateList = response.data.data;
                            
                        } else {
                            this.$Message.error(response.data.info);
                            this.cateList = []
                        }
                        resolve();
                    }).catch(error => {
                        reject(error);
                    });
                });
            },
            PermissionGet(){               
                new Promise((resolve, reject) => {
                    PermissionGetAll().then(response => {
                        if (response.data.err == 0) {
                            this.permission_list = response.data.data
                        } else {
                            
                        }
                        resolve();
                    }).catch(error => {
                        reject(error);
                    });
                });
            },
        // 页码改变
            pageChange(page) {
                this.p = page;console.log(page)
                this.getList()
            },
            // 每页条数改变
            pageSizeChange(pagesize) {
                this.pagesize = pagesize;console.log(pagesize)
                this.getList();
            },
            // 弹出框角色idname传值
            // setOption(value,type){
            //     this.org.role_id = value.value;
            //     this.org.role_name = value.label;
            // },
            // 点击新增跳转到新增页面
            addnewdoclist(){
                this.$router.push({name:'org_newdoclist'})
            },
            /**@augments
             * 
             */
            // 点击搜索,进行数据搜索
            search(){
                this.formdata.p=1;
                new Promise((resolve, reject) => {
                    get_list(this.formdata).then(response => {
                        if (response.data.err == 0) {
                            // this.$Message.success(response.data.info);
                            this.policylist = response.data.data.data;
                            this.detailTotal = response.data.data.total;
                            this.$refs['pages'].currentPage = 1 
                        } else {
                            // this.$Message.error(response.data.info);
                            this.policylist = []
                            this.detailTotal = 0
                        }
                        resolve();
                    }).catch(error => {
                        reject(error);
                    });
                });
            },
            //获取页面信息getList
            getList(){
                this.formdata.p=this.p;
                this.formdata.pagesize=this.pagesize;  console.log(this.formdata,"...")        
                new Promise((resolve, reject) => {
                    get_list(this.formdata).then(response => {
                        if (response.data.err == 0) {
                            this.policylist = response.data.data.data;
                            this.detailTotal = response.data.data.total;
                            for(var i in this.policylist){
                                if(this.policylist[i].agency_code==""||this.policylist[i].agency_code==null){
                                    // console.log(this.policylist[i],"this.policylist[i].agency_code222222222",typeof this.policylist[i].agency_code)
                                    this.policylist[i].agency_code=='无';
                                    // console.log( this.policylist[i].agency_code,"22222222222")
                                }
                            }
                        } else {
                            this.$Message.error(response.data.info);
                            this.detailTotal = 0;
                            this.policylist = []
                        }
                        resolve();
                    }).catch(error => {
                        reject(error);
                    });
                });
            },
            
            // 选中表单的其中一项,进行数据删除处理
            //选中当前行数据
            tableCurrentChange(currentRow,oldCurrentRow){
                    this.tableaction = false;
                    this.current = currentRow;
                    console.log(this.current,'当前数据信息')
                    this.current.mogon_id=currentRow._id
            },
            //点击编辑
            toDetail(){
                this.$router.push({name:'org_doclistdetail',params:{mongo_id:this.current.mogon_id}});
            },
            // 点击删除执行删除选中的table操作
            remove() {
                this.$Modal.confirm({
                    title: '提示',
                    content: '是否要删除该条记录?',
                    onOk: () => {
                        var _param = {
                            mongo_id:this.current._id
                        }
                        new Promise((resolve, reject) => {
                            delete_org(_param).then(response => {
                                console.log(response.data,"查看状态",response.data.info)
                                if(response.data.err == 0){
                                     this.$Message.success(response.data.info);
                                    //  this.getList();
                                     location.reload();
                                }else {
                                    this.$Message.error(response.data.info);
                                }                        
                                resolve();
                            }).catch(error => {
                                reject(error);
                            });
                        });
                    },
                    
                });
            },
            // 点击设置显示模态框
            transForm(){
                this.showAddModal = true;
            },
            add(){
                this.$refs['org'].validate((valid) => {
                    if (valid) {
                         this.$Modal.confirm({
                            title: '确认',
                            content: '<p>确认转移该机构下的所有数据？</p>',
                            onOk: () => {
                                this.org.org_from = this.current.id
                                new Promise((resolve, reject) => {
                                    transferData(this.org).then(response => {
                                        if (response.data.err == 0) {
                                            this.showAddModal = false;
                                            this.$Message.success(response.data.info);
                                            this.$refs["org"].resetFields();
                                            this.getList()
                                        } else {
                                            this.$Message.error(response.data.info);
                                            this.loading = false;
                                        }
                                        resolve();
                                    }).catch(error => {
                                        reject(error);
                                    });
                                });
                            },
                            onCancel: () => {
                                //this.showAddModal = false;
                                setTimeout(() => {
                                    this.loading = false;
                                    this.$nextTick(() => {
                                        this.loading = true;
                                        this.showAddModal = true;   
                                    });
                                }, 2000);
                            }
                        })             
                    } else {
                        // 通过loading来阻止modal隐藏，然后手动修改loading来充值确定按钮
                        setTimeout(() => {
                            this.loading = false;
                            this.$nextTick(() => {
                                this.loading = true;
                                this.showAddModal = true;   
                            });
                        }, 2000);
                    }    
                })  
            
            },
            cancel () {
                this.$refs['org'].resetFields(); 
            },
        },
    }
</script>